﻿using ConfExpert.Forms.Base;
using DevTeam;

using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Espresso
{
    public partial class Espresso : XForm
    {
        public Espresso()
        {
            InitializeComponent();
            TAM.ESP2TableAdapter = new Datasets.MyDataTableAdapters.ESP2TableAdapter();
            TAM.ESP2LinesTableAdapter = new Datasets.MyDataTableAdapters.ESP2LinesTableAdapter();
        }

        string parite()
        {
            IFormatProvider iFormatProvider = System.Globalization.CultureInfo.InvariantCulture;
            //new System.Globalization.CultureInfo("es-ES");
            //string s2 = string.Format(iFormatProvider, "{0:#,##0.000}", val)
            //Note that the , and . are using a 'standard' en-US style, but .ToString() and string.Format() with a format provider does the right thing.

            //return (Session.Eur / Session.Usd).ToString("#,##0.000", iFormatProvider);
            return (1.4).ToString("#,##0.000", iFormatProvider);
        }

        BindingSource bs, bsLines;
        bool EstiCemal;
        private void Espresso_Load(object sender, EventArgs e)
        {

            EstiCemal = ytk(118);

            ds.ESP2Lines.Columns.Add("Eur2Usd", typeof(decimal), parite());
            ds.ESP2Lines.Columns.Add("IcPiyasaEurMaliyet", typeof(decimal), "Iif(OriginId = 3, Price*Iif(DvzId = 1, 1, 1/Eur2Usd), ((Price*Iif(DvzId = 1, Eur2Usd, 1) + Iif(TermsId = 1, 0.07, 0)) * 1.35 * 1.06)/Eur2Usd)");
            ds.ESP2Lines.Columns.Add("IhracatEurMaliyet", typeof(decimal), "Iif(OriginId = 3, Price*Iif(DvzId = 1, 1, 1/Eur2Usd), ((Price*Iif(DvzId = 1, Eur2Usd, 1) + Iif(TermsId = 1, 0.07, 0)) * 1.15 * 1.06)/Eur2Usd)");

            ds.ESP2.Columns.Add("POCount", typeof(int), "COUNT(Child(FK_ESP2Lines_ESP2_idx).idx2)");

            bs = new BindingSource(ds, "ESP2");
            bsLines = new BindingSource(bs, "FK_ESP2Lines_ESP2_idx");
            AddBsToList(bs, bsLines);
            InitGridView(gridView1, bs);
            InitGridView(gridView2, bsLines);

            gridView1.MakeColumnInvisible("idx", "Booklet", "ProductId");

            gridView2.MakeColumnInvisible("idx", "idx2", "Eur2Usd");

            foreach (string str in "IcPiyasaEurMaliyet;IhracatEurMaliyet".Split(';'))
            {
                gridView1.Columns[str].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                gridView1.Columns[str].DisplayFormat.FormatString = "€ #,###.00";

                gridView2.Columns[str].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                gridView2.Columns[str].DisplayFormat.FormatString = "€ #,###.00";
            }

            gridView1.Columns["POCount"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            gridView1.Columns["POCount"].DisplayFormat.FormatString = "#,###";

            AddAverage(gridView1, "IcPiyasaEurMaliyet", "IhracatEurMaliyet");
            AddAverage(gridView2, "IcPiyasaEurMaliyet", "IhracatEurMaliyet");
            gridView1.AddSummary("POCount");

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnKaydet.Caption = EstiCemal ? "Kaydet" : "Güncelle";
            gridView1.OptionsBehavior.Editable = gridView2.OptionsBehavior.Editable = EstiCemal;
            gridView1.OptionsView.NewItemRowPosition = EstiCemal ? DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom : DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView2.OptionsView.NewItemRowPosition = EstiCemal ? DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom : DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

            gridView1.SetViewCaption("Espresso artikelleri");
            gridView2.SetViewCaption("Seçili artikelin alımları");

            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridView1_FocusedRowChanged);

            Exp(true);
            splitContainerControl1.AutoSize();
            //ExpandCollapse(splitContainerControl1, bsLines.Count > 0 ? AcKapa.Expand : AcKapa.Collapse);
            GetDetails("ESP2;ESP2Lines", "GetESP2");
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (btnKaydet.Caption == "Kaydet")
            {
                try
                {
                    Exp(false);
                    Save();
                }
                finally
                {
                    Exp(true);
                }
            }
            else
            {
                //ds.EspMaliyet.Clear();
                //gridView1.Columns["QtyAvailable"].Visible = gridView1.Columns["ProductId"].Visible = gridView1.Columns["Scan"].Visible = chkArtikel.Checked;
                GetDetails("ESP2;ESP2Lines", "GetESP2");
                gridView1.BestFitColumns();
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtInfo.Caption = gridView1.GetFocusedRowCellDisplayText("EspNr");
            //ExpandCollapse(splitContainerControl1, bsLines.Count > 0 ? AcKapa.Expand : AcKapa.Collapse);
        }


        void Exp(bool flag)
        {
            if (flag)
            {
                ds.ESP2Lines.Columns["Eur2Usd"].Expression = parite();
                ds.ESP2Lines.Columns["IcPiyasaEurMaliyet"].Expression = "Iif(OriginId = 3, Price*Iif(DvzId = 1, 1, 1/Eur2Usd), ((Price*Iif(DvzId = 1, Eur2Usd, 1) + Iif(TermsId = 1, 0.07, 0)) * 1.35 * 1.06)/Eur2Usd)";
                ds.ESP2Lines.Columns["IhracatEurMaliyet"].Expression = "Iif(OriginId = 3, Price*Iif(DvzId = 1, 1, 1/Eur2Usd), ((Price*Iif(DvzId = 1, Eur2Usd, 1) + Iif(TermsId = 1, 0.07, 0)) * 1.15 * 1.06)/Eur2Usd)";
                ds.ESP2.Columns["POCount"].Expression = "COUNT(Child(FK_ESP2Lines_ESP2_idx).idx2)";
                ds.ESP2.Columns["IcPiyasaEurMaliyet"].Expression = "AVG(Child(FK_ESP2Lines_ESP2_idx).IcPiyasaEurMaliyet)";
                ds.ESP2.Columns["IhracatEurMaliyet"].Expression = "AVG(Child(FK_ESP2Lines_ESP2_idx).IhracatEurMaliyet)";
            }
            else
                ds.ESP2Lines.Columns["IcPiyasaEurMaliyet"].Expression = ds.ESP2Lines.Columns["IhracatEurMaliyet"].Expression = ds.ESP2Lines.Columns["Eur2Usd"].Expression = ds.ESP2.Columns["POCount"].Expression = ds.ESP2.Columns["IcPiyasaEurMaliyet"].Expression = ds.ESP2.Columns["IhracatEurMaliyet"].Expression = null;

        }

    }
}